<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Product_Model extends MY_Model {
    public function __construct() {
        parent::__construct();
        $this->load->model('entity/product_entity', 'Product_Entity');
        $this->load->model('entity/product_details_entity', 'Product_Details_Entity');
    }

    public function getProductById($id) {
        $select = "
        a.id          AS product_id,
        a.cate_id     AS product_cate_id,
        a.shop_id     AS product_shop_id,
        a.title       AS product_title,
        a.description AS product_description,
        a.slug        AS product_slug,
        b.image       AS product_image,
        a.price_old   AS product_price_old,
        a.price_new   AS product_price_new,
        c.title       AS cate_title,
        c.slug        AS cate_slug,
        e.image_folder AS shop_image_folder
        ";
        $this->db->select($select, true);
        $this->db->from($this->table_product_name . ' AS a');
        $this->db->join($this->table_product_details_name . ' b', 'a.id=b.product_id');
        $this->db->join($this->table_category_name . ' c', 'a.cate_id=c.id');
        $this->db->join($this->table_shop_name . ' d', 'a.shop_id=d.id');
        $this->db->join($this->table_user_name . ' e', 'd.user_id=e.id');
        $this->db->where('a.id', $id);
        $this->db->where('a.active_flg', 1);
        $this->db->where('a.deleted_at', null);
        $this->db->where('b.is_avatar', 1);
        $this->db->where('b.deleted_at', null);
        $this->db->where('c.deleted_at', null);
        $this->db->where('d.deleted_at', null);
        $this->db->where('e.deleted_at', null);
        return $this->db->get()->first_row();
    }

    public function getProduct($slug, $option=array()) {
        $select = "
        a.id          AS product_id,
        a.cate_id     AS product_cate_id,
        a.shop_id     AS product_shop_id,
        a.title       AS product_title,
        a.slug        AS product_slug,
        a.active_flg  AS product_active_flg,
        a.deleted_at  AS product_deleted_at,
        b.description AS product_description,
        b.image       AS product_image,
        b.price_old   AS product_price_old,
        b.price_new   AS product_price_new,
        c.title       AS cate_title,
        c.slug        AS cate_slug,
        d.title       AS shop_title,
        d.slug        AS shop_slug,
        e.image_folder AS shop_image_folder
        ";
        $this->db->select($select, true);
        $this->db->from($this->table_product_name . ' AS a');
        $this->db->join($this->table_product_details_name . ' b', 'a.id=b.product_id');
        $this->db->join($this->table_category_name . ' c', 'a.cate_id=c.id');
        $this->db->join($this->table_shop_name . ' d', 'a.shop_id=d.id');
        $this->db->join($this->table_user_name . ' e', 'd.user_id=e.id');
        if(count($option)>0){
            $this->db->where($option);
        } else {
            $this->db->where('a.slug', $slug);
            $this->db->where('a.active_flg', 1);
            $this->db->where('a.deleted_at', null);
            $this->db->where('b.is_avatar', 1);
            $this->db->where('b.deleted_at', null);
            $this->db->where('c.deleted_at', null);
            $this->db->where('d.deleted_at', null);
            $this->db->where('e.deleted_at', null);
        }
        return $this->db->get()->first_row();
    }

    public function getLatestProducts($aAddColumn=array(), $options=array()) {
        $select = null;
        if(count($aAddColumn)!=0){
            $select = implode(',', $aAddColumn);
        } else {
           $select = 'a.id, a.title, a.slug, a.sale_flg, b.image, b.is_avatar, b.id as detail_id, b.code,b.price_new, b.price_old, e.image_folder as shop_image_folder';
        }
        $this->db->select($select, true);
        $this->db->from($this->table_product_name . ' AS a');
        $this->db->join($this->table_product_details_name . ' b', 'a.id=b.product_id');
        $this->db->join($this->table_category_name . ' c', 'a.cate_id=c.id');
        $this->db->join($this->table_shop_name . ' d', 'a.shop_id=d.id');
        $this->db->join($this->table_user_name . ' e', 'd.user_id=e.id');
        if(count($options) > 0){
            $this->db->where($options);
        } else {
            $this->db->where('a.home_flg', ON);
            $this->db->where('a.active_flg', ON);
            $this->db->where('a.deleted_at', null);
            $this->db->where('b.is_avatar', ON);
            $this->db->where('b.deleted_at', null);
            $this->db->where('c.deleted_at', null);
            $this->db->where('d.deleted_at', null);
        }
        $this->db->order_by('a.created_at', 'desc');
        $this->db->order_by('a.updated_at', 'desc');
        $this->db->limit(PRODUCT_LIMIT);
        $result = $this->db->get()->result('Product_Entity');
        return $result;
    }

    public function getProductDetails($product_id, $isAvatar = false) {
        $select = 'b.id, b.product_id, b.code, b.is_avatar, b.price_old, b.price_new, b.image, b.description';
        $this->db->select($select, true);
        $this->db->from($this->table_product_name . ' AS a');
        $this->db->join($this->table_product_details_name . ' b', 'a.id=b.product_id');
        $this->db->where('a.deleted_at', null);
        $this->db->where('b.deleted_at', null);
        $this->db->where('b.product_id', $product_id);
        $result = null;
        if ($isAvatar) {
            $this->db->where('b.is_avatar', 1);
            $result = $this->db->get()->first_row('Product_Details_Entity');
        } else {
            $this->db->order_by('b.is_avatar', 'desc');
            $result = $this->db->get()->result('Product_Details_Entity');
        }
        return $result;
    }

    public function getProductDetailInfo($product_details_id) {
        $result = null;
        $select = 'a.slug, a.title, c.title as category_title, a.shop_id, c.slug as category_slug,
        b.id, b.product_id, b.code, b.is_avatar, b.price_old, b.price_new, b.image, b.description,
        d.title as shop_title, d.slug as shop_slug, e.image_folder AS shop_image_folder, e.email AS shop_email';
        $this->db->select($select, true);
        $this->db->from($this->table_product_name . ' AS a');
        $this->db->join($this->table_product_details_name . ' b', 'a.id=b.product_id');
        $this->db->join($this->table_category_name . ' c', 'a.cate_id=c.id');
        $this->db->join($this->table_shop_name . ' d', 'a.shop_id=d.id');
        $this->db->join($this->table_user_name . ' e', 'd.user_id=e.id');
        $this->db->where('a.deleted_at', null);
        $this->db->where('b.deleted_at', null);
        $this->db->where('c.deleted_at', null);
        $this->db->where('d.deleted_at', null);
        $this->db->where('e.deleted_at', null);
        $this->db->where('b.id', $product_details_id);
        $result = $this->db->get()->first_row('Product_Details_Entity');
        return $result;
    }

    public function getCategoryProducts($cateId) {
        $result = null;
        $this->db->select('
            a.id as product_id, a.title, a.cate_id, a.shop_id, a.slug,
            b.id, b.id as detail_id, b.code, b.image, b.price_new, b.price_old, b.description,
            c.slug AS shop_slug, e.image_folder AS shop_image_folder', true
        );
        $this->db->from($this->table_product_name . ' AS a');
        $this->db->join($this->table_product_details_name . ' b', 'a.id=b.product_id');
        $this->db->join($this->table_shop_name . ' c', 'a.shop_id=c.id');
        $this->db->join($this->table_category_name . ' d', 'a.cate_id=d.id');
        $this->db->join($this->table_user_name . ' e', 'c.user_id=e.id');
        $this->db->where('a.active_flg', 1);
        $this->db->where('a.deleted_at', null);
        $this->db->where('b.is_avatar', 1);
        $this->db->where('b.deleted_at', null);
        $this->db->where('c.deleted_at', null);
        $this->db->where('d.deleted_at', null);
        $this->db->where('e.deleted_at', null);
        $this->db->where('a.cate_id', $cateId);
        $this->db->order_by('a.updated_at', 'asc');
        $this->db->order_by('a.created_at', 'asc');
        $result = $this->db->get()->result('Product_Details_Entity');
        return $result;
    }

    public function getProductList($limit, $offset, $option=array()) {
        $select = "a.id as product_id, a.title, a.title as product_title, a.slug, a.slug as product_slug, a.sale_flg, a.home_flg, a.cate_id as category_id, a.shop_id as shop_id,
        a.created_at as product_created_at, a.highlight_flg as product_highlight_flg, a.active_flg as product_active_flg, a.deleted_at as product_deleted_at,
        b.id as detail_id, b.code, b.image, b.price_new, b.price_old,
        c.title as shop_title, c.slug as shop_slug, e.image_folder AS shop_image_folder,
        d.title as category_title, d.slug as category_slug";
        $result = null;
        $this->db->select($select, true);
        $this->db->from($this->table_product_name . ' AS a');
        $this->db->join($this->table_product_details_name . ' b', 'a.id=b.product_id');
        $this->db->join($this->table_shop_name . ' c', 'a.shop_id=c.id');
        $this->db->join($this->table_category_name . ' d', 'a.cate_id=d.id');
        $this->db->join($this->table_user_name . ' e', 'c.user_id=e.id');
        if(count($option) >0){
            $this->db->where($option);
        }
        $this->db->order_by('a.created_at', 'desc');
        $this->db->order_by('a.updated_at', 'desc');
        $this->db->order_by('a.id', 'desc');
        $this->db->limit($limit, $offset);
        $result = $this->db->get()->result('Product_Details_Entity');
        return $result;
    }

    /**
     * For admin only
    */
    public function countAllProduct($option=array()) {
        $this->db->select('a.id', true);
        $this->db->from($this->table_product_name . ' AS a');
        $this->db->join($this->table_product_details_name . ' b', 'a.id=b.product_id');
        $this->db->join($this->table_shop_name . ' c', 'a.shop_id=c.id');
        $this->db->join($this->table_category_name . ' d', 'a.cate_id=d.id');
        $this->db->join($this->table_user_name . ' e', 'c.user_id=e.id');
        if(count($option) >0){
            $this->db->where($option);
        }
        return $this->db->count_all_results();
    }

    /**
     * For admin only
    */
    public function countAllSearchProduct($key) {
        $select = "a.id as product_id, a.title as product_title, a.slug as product_slug,
        a.cate_id as category_id, a.shop_id as shop_id,
        a.created_at as product_created_at, a.active_flg as product_active_flg,
        a.deleted_at as product_deleted_at,
        b.code, b.image, b.price_new, b.price_old,
        c.title as shop_title,
        d.title as category_title, d.slug as category_slug";
        $result = null;
        $this->db->select($select, true);
        $this->db->from($this->table_product_name . ' AS a');
        $this->db->join($this->table_product_details_name . ' b', 'a.id=b.product_id');
        $this->db->join($this->table_shop_name . ' c', 'a.shop_id=c.id');
        $this->db->join($this->table_category_name . ' d', 'a.cate_id=d.id');
        $likeq = "a.title LIKE '%{$key}%'";
        $likeq .= " OR a.slug LIKE '%{$key}%'";
        $likeq .= " OR b.code LIKE '%{$key}%'";
        $likeq .= " OR b.price_old LIKE '%{$key}%'";
        $likeq .= " OR b.price_new LIKE '%{$key}%'";
        $likeq .= " OR b.description LIKE '%{$key}%'";
        $likeq .= " OR c.title LIKE '%{$key}%'";
        $likeq .= " OR c.slug LIKE '%{$key}%'";
        $likeq .= " OR d.title LIKE '%{$key}%'";
        $likeq .= " OR d.slug LIKE '%{$key}%'";
        $likeq = "(" . $likeq . ")";
        $this->db->where($likeq);
        return $this->db->count_all_results();
    }

    /**
     * For admin only
    */
    public function searchProductAdmin($limit, $offset, $key) {
        $select = "a.id as product_id, a.title as product_title, a.slug as product_slug,
        a.cate_id as category_id, a.shop_id as shop_id,
        a.created_at as product_created_at, a.highlight_flg as product_highlight_flg, a.active_flg as product_active_flg,
        a.deleted_at as product_deleted_at,
        b.code, b.image, b.price_new, b.price_old,
        c.slug as shop_slug, c.title as shop_title, e.image_folder AS shop_image_folder,
        d.title as category_title, d.slug as category_slug";
        $result = null;
        $this->db->select($select, true);
        $this->db->from($this->table_product_name . ' AS a');
        $this->db->join($this->table_product_details_name . ' b', 'a.id=b.product_id');
        $this->db->join($this->table_shop_name . ' c', 'a.shop_id=c.id');
        $this->db->join($this->table_category_name . ' d', 'a.cate_id=d.id');
        $this->db->join($this->table_user_name . ' e', 'c.user_id=e.id');
        $likeq = "a.title LIKE '%{$key}%'";
        $likeq .= " OR a.slug LIKE '%{$key}%'";
        $likeq .= " OR b.code LIKE '%{$key}%'";
        $likeq .= " OR b.price_old LIKE '%{$key}%'";
        $likeq .= " OR b.price_new LIKE '%{$key}%'";
        $likeq .= " OR b.description LIKE '%{$key}%'";
        $likeq .= " OR c.title LIKE '%{$key}%'";
        $likeq .= " OR c.slug LIKE '%{$key}%'";
        $likeq .= " OR d.title LIKE '%{$key}%'";
        $likeq .= " OR d.slug LIKE '%{$key}%'";
        $likeq = "(" . $likeq . ")";
        $this->db->where($likeq);
        $this->db->order_by('a.id');
        $this->db->limit($limit, $offset);
        $result = $this->db->get()->result('Product_Details_Entity');
        return $result;
    }

    public function getProductInSet($product_id) {
        $result = null;
        $select = "b.id, b.code, b.image, b.price_new, b.price_old, b.description, b.is_avatar, b.deleted_at";
        $this->db->select($select, true);
        $this->db->from($this->table_product_details_name . ' b');
        $this->db->where('b.product_id', $product_id);
        $result = $this->db->get()->result('Product_Details_Entity');
        return $result;
    }

    public function updateProduct($id, $options) {
        $this->db->trans_begin();
        $this->db->where('id', $id);
        $this->db->update($this->table_product_name, $options);
        if(isset($options['highlight_flg'])){
            $this->db->where('product_id', $id);
            $this->db->update($this->table_product_highlight_name, array('deleted_at'=>$options['updated_at']));
            if($options['highlight_flg']==ON){
                $this->db->insert($this->table_product_highlight_name, array('product_id'=>$id, 'created_at'=>$options['updated_at']));
            }
        }
        if ($this->db->trans_status() === false) {
            $this->db->trans_rollback();
            return false;
        } else {
            $this->db->trans_commit();
        }
        return true;
    }
}
?>